<template>
  <el-container class="admin-layout">
    <!-- 侧边栏 -->
    <el-aside :width="collapsed ? '64px' : '240px'" class="sidebar">
      <div class="sidebar-header">
        <h2 v-if="!collapsed">管理员后台</h2>
        <span v-else class="logo-mini">🐾</span>
      </div>
      
      <el-menu
        :default-active="$route.path"
        class="el-menu-vertical"
        :collapse="collapsed"
        background-color="#2c3e50"
        text-color="rgba(255, 255, 255, 0.7)"
        active-text-color="#ffffff"
        router
      >
        <el-menu-item v-for="item in menuItems" :key="item.path" :index="item.path">
          <el-icon><component :is="item.icon" /></el-icon>
          <template #title>{{ item.name }}</template>
        </el-menu-item>
        
        <el-menu-item @click="logout" index="logout">
          <el-icon><SwitchButton /></el-icon>
          <template #title>退出登录</template>
        </el-menu-item>
      </el-menu>
      
      <!-- 折叠按钮 -->
      <div class="collapse-btn" @click="toggleSidebar">
        <el-icon v-if="collapsed"><ArrowRight /></el-icon>
        <el-icon v-else><ArrowLeft /></el-icon>
      </div>
    </el-aside>
    
    <!-- 主内容区 -->
    <el-container>
      <el-header class="header">
        <div class="header-left">
          <el-breadcrumb separator="/">
            <el-breadcrumb-item :to="{ path: '/admin' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item v-if="$route.meta && $route.meta.title">{{ $route.meta.title }}</el-breadcrumb-item>
          </el-breadcrumb>
        </div>
        <div class="header-right">
          <el-dropdown trigger="click">
            <span class="user-dropdown">
              管理员 <el-icon class="el-icon--right"><ArrowDown /></el-icon>
            </span>
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item @click="logout">退出登录</el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </div>
      </el-header>
      
      <el-main>
        <router-view />
      </el-main>
    </el-container>
  </el-container>
</template>

<script setup>
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessageBox } from 'element-plus'
import {
  House,
  Box,
  Document,
  User,
  DataAnalysis,
  SwitchButton,
  ArrowRight,
  ArrowLeft,
  ArrowDown
} from '@element-plus/icons-vue'

const router = useRouter()
const collapsed = ref(false)

const toggleSidebar = () => {
  collapsed.value = !collapsed.value
}

const logout = () => {
  ElMessageBox.confirm(
    '确定要退出登录吗？',
    '提示',
    {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning',
    }
  ).then(() => {
    // 清除token
    localStorage.removeItem('token')
    // 跳转到登录页
    router.push('/')
  }).catch(() => {
    // 取消退出
  })
}

const menuItems = [
  { 
    name: '首页', 
    path: '/admin', 
    icon: House
  },
  { 
    name: '库存管理', 
    path: '/admin/inventory', 
    icon: Box
  },
  { 
    name: '订单管理', 
    path: '/admin/orders', 
    icon: Document
  },
  { 
    name: '用户管理', 
    path: '/admin/users', 
    icon: User
  },
  { 
    name: '统计报表', 
    path: '/admin/reports', 
    icon: DataAnalysis
  }
]
</script>

<style scoped>
.admin-layout {
  height: 100vh;
}

.sidebar {
  transition: width 0.3s;
  position: relative;
  background-color: #2c3e50;
  overflow: hidden;
}

.sidebar-header {
  height: 60px;
  padding: 0 20px;
  display: flex;
  align-items: center;
  justify-content: center;
  background-color: #1a252f;
}

.sidebar-header h2 {
  margin: 0;
  font-size: 1.2rem;
  font-weight: 600;
  color: white;
}

.logo-mini {
  font-size: 1.5rem;
}

.el-menu-vertical:not(.el-menu--collapse) {
  width: 240px;
}

.collapse-btn {
  position: absolute;
  bottom: 20px;
  right: 20px;
  width: 30px;
  height: 30px;
  border-radius: 50%;
  background-color: #1a252f;
  color: rgba(255, 255, 255, 0.7);
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  z-index: 10;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.12);
  transition: all 0.3s;
}

.collapse-btn:hover {
  background-color: #409EFF;
  color: white;
}

.header {
  background-color: #fff;
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.08);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 20px;
}

.header-left, .header-right {
  display: flex;
  align-items: center;
}

.user-dropdown {
  cursor: pointer;
  color: #606266;
  display: flex;
  align-items: center;
}

.el-main {
  background-color: #f0f2f5;
  padding: 20px;
}
</style> 